/**
 * Created by Jacky.Gao on 2017-01-25.
 */
import {alert} from '../MsgBox.js';

import JSEncrypt from "jsencrypt";

export default class Tool {
    constructor(context) {
        this.context = context;
    }

    buildButton() {
        const btn = $(`<button type="button" class="btn btn-default" style="border:none;border-radius:0;background: #f8f8f8;padding: 6px 5px;" title="${this.getTitle()}">
             ${this.getIcon()}
        </button>`);
        const _this = this;
        btn.click(function () {
            _this.execute();
        });
        return btn;
    }

    checkSelection() {
        const selected = this.context.hot.getSelected();
        if (!selected || selected.length === 0) {
            alert(`${window.i18n.selectTargetCellFirst}`);
            return false;
        } else {
            return true;
        }
    }
};

const splitIntoChunks = (str, chunkSize) => {
    const chunks = [];
    for (let i = 0; i < str.length; i += chunkSize) {
        chunks.push(str.substring(i, i + chunkSize));
    }
    return chunks;
};

// AES加密函数
export function encryptData(data) {
    const encrypt = new JSEncrypt();
    encrypt.setPublicKey(
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0rpIcEwVLxcQ08Y2butm2K2zqveDHLm9ZoebPixxcQ4TuiXiKNVfSviELXt2maChi1Fqxsgydx8EPnDfUB84bomLdJUbF9TTHkhqgG9yG4VMTZWQ9LpJaIa4lrSYELcoZWFGe6JIfQDierGPdkfWpm982ahT0zVb3hK341I5wewIDAQAB"
    );
    // 将数据分块加密
    const chunkSize = 20; // RSA 最大加密长度（根据密钥长度调整）
    const chunks = splitIntoChunks(data, chunkSize);

    // 对每个块进行加密
    const encryptedChunks = chunks.map((chunk) => encrypt.encrypt(chunk));

    // 返回加密后的块，用逗号连接
    return encryptedChunks.join(",");
};

